package com.istudy.dao.impl;

import com.istudy.dao.interfaces.UserDao;
import com.istudy.entity.User;

import com.istudy.dao.BaseDao;
import com.istudy.entity.UserType;
import com.istudy.utils.Page;
import pers.dzk.jdk.dao.AutoBaseDao;
import pers.dzk.jdk.entity.PageBean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserImp extends AutoBaseDao<User> implements UserDao {

    public UserImp(Connection conn) {
        super(conn);
    }

    @Override
    public List<UserType> selectUserType() {

        String sql="select * from user_type";
        List<UserType> list=new ArrayList();
        try{
            ResultSet set = query(sql,list.toArray());
            while(set.next()){
                UserType ut = new UserType();
                ut.setUserTypeId(set.getInt("user_type_id"));
                ut.setUserTypeName(set.getString("user_type_name"));
                list.add(ut);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int insertUser(User user) {
        int i ;
        try {
            i = update("INSERT `user` (user_login_name,user_name,user_password,user_type_id,user_money) VALUES(?,?,?,?,?);",user.getUserLoginName(),user.getUserName(),user.getUserPassword(),user.getUserType(),user.getUserMoney());
        } catch (SQLException e) {
            e.printStackTrace();
            i = -1;
        }
        return i;
    }

    @Override
    public User getUserById(int id) {
        User user1=new User();
        String sql="select * from user where `user_id`= ?";
        BaseDao baseDao=new BaseDao();
        List<Object> list=new ArrayList<>();
        list.add(id);
        BaseDao.getConnection();
        ResultSet set =baseDao.executeQuery(sql,list.toArray());
        try{
        while(set.next()){
            user1.setUserId(set.getInt("user_Id"));
            user1.setUserType(set.getInt("user_type_id"));
            user1.setUserName(set.getString("user_Name"));
            user1.setUserLoginName(set.getString("user_login_name"));
            user1.setUserPassword(set.getString("user_password"));
            user1.setUserMoney(set.getInt("user_Money"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
        user1 = null;
    }
        if(user1.getUserId() == null){
        user1 = null;
    }
        return user1;
}

    @Override
    public User selectUserByUserLoginNameAndUserPassword(User user) {
        User user1=new User();
//        try {
////            user1 = select(User.class,"select * from user where user_login_name = ? and user_password = ?",user.getUserLoginName(),user.getUserPassword()).get(0);
//        } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException | NoSuchFieldException | SQLException e) {
//            e.printStackTrace();
//            user1 = null;
//        }


        try {
            String sql="select * from user where user_login_name = ? and user_password = ?";
            BaseDao baseDao=new BaseDao();
            List<Object> list=new ArrayList<>();
            list.add(user.getUserLoginName());
            list.add(user.getUserPassword());
            BaseDao.getConnection();
            ResultSet set =baseDao.executeQuery(sql,list.toArray());
            while(set.next()){
              user1.setUserId(set.getInt("user_Id"));
              user1.setUserType(set.getInt("user_type_id"));
                user1.setUserName(set.getString("user_Name"));
              user1.setUserLoginName(set.getString("user_login_name"));
              user1.setUserPassword(set.getString("user_password"));
              user1.setUserMoney(set.getInt("user_Money"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            user1 = null;
        }
        if(user1.getUserId() == null){
            user1 = null;
        }
        return user1;
    }

    @Override
    public User selectUserByLoginName(User user) {
        User user1 = null;
        try {
            String sql="select * from user where user_login_name = ?";
            ResultSet rs = query(sql,user.getUserLoginName());
            while(rs.next()){
                User user2 = new User();
                user2.setUserId(rs.getInt("user_Id"));
                user2.setUserType(rs.getInt("user_Type_id"));
                user2.setUserName(rs.getString("user_Name"));
                user2.setUserLoginName(rs.getString("user_login_name"));
                user2.setUserPassword(rs.getString("user_password"));
                user2.setUserMoney(rs.getInt("user_Money"));
                user1 = user2;
            }
        }catch (Exception e){
            e.printStackTrace();
            user1 = null;
        }
        return user1;
    }

    @Override
    public PageBean<User> select(PageBean<User> pageBean) {
        String sql = "select user.user_login_name,user.user_money,user.user_id,user.user_password,user.user_name,user_type.user_type_name from user inner join user_type on user.user_type_id=user_type.user_type_id ORDER BY  user.user_id limit %s,%s";
        String pagingSql = String.format(sql,(pageBean.getCurrPageNo()-1)*pageBean.getPageSize(),pageBean.getPageSize());
        String countSql = String.format("select count(*) from user");
        try {
            setRs(query(countSql));
            getRs().next();
            int count = getRs().getInt(1);
            pageBean.setAllCount(count);
            setRs(query(pagingSql));
            List list = new ArrayList();
            while (getRs().next()){
                User user2 = new User();
                user2.setUserId(getRs().getInt("user_Id"));
                user2.setUserTypeName(getRs().getString("user_type_name"));
                user2.setUserName(getRs().getString("user_Name"));
                user2.setUserLoginName(getRs().getString("user_login_name"));
                user2.setUserPassword(getRs().getString("user_password"));
                user2.setUserMoney(getRs().getInt("user_Money"));
                list.add(user2);
            }
            pageBean.setList(list);
            return pageBean;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer queryCount() throws SQLException {
        return null;
    }

    @Override
    public List<User> Getpartall(Page page) throws SQLException {
        return null;
    }

    @Override
    public Integer AddUaer(User user) {
        return null;
    }

    @Override
    public User UserGetById(Integer id) throws SQLException {
        return null;
    }

    @Override
    public Integer Userupdate(User user) {
        return null;
    }

    @Override
    public Integer deleteUser(Integer id) {
        return null;
    }

    @Override
    public User selectByUserId(User user) {
        String sql = "select * from user where user_id = ?";
        User user2 = new User();
        try {
            ResultSet rs = query(sql,user.getUserId());
            while (rs.next()){
                user2.setUserId(getRs().getInt("user_Id"));
                user2.setUserType(getRs().getInt("user_Type_id"));
                user2.setUserName(getRs().getString("user_Name"));
                user2.setUserLoginName(getRs().getString("user_login_name"));
                user2.setUserPassword(getRs().getString("user_password"));
                user2.setUserMoney(getRs().getInt("user_Money"));
            }
            return user2;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateUser(User user) {
        int i ;
        String sql = "UPDATE `user` SET user_login_name = ?,user_name = ?,user_type_id = ?,user_money = ?\n" +
                " WHERE user_id = ?;\n";
        try {
            i = update(sql,user.getUserLoginName(),user.getUserName(),user.getUserType(),user.getUserMoney(),user.getUserId());
        } catch (SQLException e) {
            e.printStackTrace();
            i = -1;
        }
        return i;
    }

    @Override
    public int updateUserByUserId(User user) {
        int i ;
        String sql = "UPDATE `user` SET user_money = ?\n" +
                " WHERE user_id = ?;\n";
        try {
            i = update(sql,user.getUserMoney(),user.getUserId());
        } catch (SQLException e) {
            e.printStackTrace();
            i = -1;
        }
        return i;
    }

    @Override
    public int deleteUserByUserId(User user) {
        int i;
        String sql = "UPDATE `user` SET user_type_id = 1\n" +
                " WHERE user_id = ?;\n";
        try {
            i = update(sql,user.getUserId());
        } catch (SQLException e) {
            e.printStackTrace();
            i = -1;
        }
        return i;
    }
}
